<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>菜单管理<small>菜单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统设置</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>

            <!--表格部分-->
            <div class="app-container">
                <div class="box">
                    <!--新建-->
                    <div class="filter-container">
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>
                    <!--表头部分-->
                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                        <el-table-column prop="linkUrl" label="链接地址" align="center"></el-table-column>
                        <el-table-column prop="path" label="排序号" align="center"></el-table-column>
                        <el-table-column prop="level" label="菜单等级" align="center"></el-table-column>
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <!--分页组件-->
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增菜单项" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" >
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item  label="链接页面">
                                            <el-input v-model="formData.linkUrl" :disabled="disabled"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单级别">
                                            <el-select @change="chooseMedicine(value)" v-model="formData.level">
                                                <el-option label="一级菜单" value="1"></el-option>
                                                <el-option label="二级菜单" value="2"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>

                                    <el-col :span="12">
                                        <el-form-item label="上级菜单">
                                            <el-select :disabled="disabled"  v-model="formData.parentMenuId">
                                                <!--遍历生成-->
                                                <template v-for="parentMenu in parentMenuList">
                                                    <el-option :label="parentMenu.name" :value="parentMenu.id"></el-option>
                                                </template>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-form-item label="图标选择">
                                        <el-select :disabled="disabled2" v-model="formData.icon" >
                                            <template v-for="icc in iconlist">
                                                <el-option :label="icc" :value="icc" :class="icc"></el-option><br>
                                            </template>
                                        </el-select>
                                    </el-form-item>

                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>

                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" >
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="链接页面"  >
                                            <el-input v-model="formData.linkUrl" :disabled="disabled"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单级别">
                                            <el-select @change="chooseMedicine" v-model="formData.level">
                                                <el-option label="一级菜单" value="1"></el-option>
                                                <el-option label="二级菜单" value="2"></el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>

                                    <el-col :span="12">
                                        <el-form-item label="上级菜单">
                                            <el-select  :disabled="disabled"  v-model="formData.parentMenuId">
                                                <!--遍历生成-->
                                                <template v-for="parentMenu in parentMenuList">
                                                    <el-option :label="parentMenu.name" :value="parentMenu.id"></el-option>
                                                </template>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-form-item label="图标选择">
                                        <el-select :disabled="disabled2" v-model="formData.icon">

                                            <template v-for="icc in iconlist">
                                                <el-option :label="icc" :value="icc" :class="icc"></el-option><br>
                                            </template>
                                        </el-select>
                                    </el-form-item>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                iconlist:[
                ],
                disabled:false,
                disabled2:false,
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
                parentMenuList:[],//以及菜单数组集合
				dataList: [],//当前页要展示的分页列表数据
                formData: {},//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisible4Edit:false,//编辑表单是否可见

                backendUrl : "http://localhost:82"
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {

                this.findPage();
                this.findIcon();
            },
            methods: {
                findIcon(){
                    axios.defaults.withCredentials = true;
                    axios.post(this.backendUrl + "/menu/icons.do").then((res)=>{

                        console.log(res.data.data);
                        this.iconlist=res.data.data;


                    }).catch((err)=>{
                        console.log(err);
                    });
                },

                chooseMedicine(){
                    console.log("这是一个测试的改变"+this.formData.level);
                    if (this.formData.level == 1){
                        this.disabled = true;
                        this.disabled2=false;
                        this.formData.linkUrl="";
                    }else{
                        this.disabled = false;
                        this.disabled2 = true;
                    }
                },
                //编辑
                handleEdit() {
                    axios.defaults.withCredentials = true;
                    axios.post(this.backendUrl + "/menu/edit.do", this.formData).then((res)=>{
                        //3.  如果修改失败，提示修改失败；如果修改不成功，提示修改成功
                        if(res.data.flag){
                            this.$message({
                                message: res.data.message,
                                type: "success"
                            });
                        }else{
                            this.$message({
                                message: res.data.message,
                                type: "error"
                            });
                        }
                        //4.关闭窗口
                        this.dialogFormVisible4Edit = false;
                        //5.分页查询数据
                        this.findPage();
                        //6.重置表单
                        this.resetForm();
                    }).catch((err)=>{
                        this.showMessage(err);
                    });
                },
                //添加

                handleAdd () {
                    axios.defaults.withCredentials = true;

                    console.log(this.formData);

                    axios.post(this.backendUrl + "/menu/add.do",this.formData).then((res)=>{

                        console.log(res.data.data);
                         this.parentMenuList=res.data.data;
                        /*设置数据*/
                        this.$message({
                            type:res.data.flag ? "success" :"error",
                            message : res.data.message
                        });
                        //4.  关闭窗口
                        this.dialogFormVisible = false;
                        //5.  分页查询数据
                        this.findPage();
                        //6.  重置表单
                        this.resetForm();

                    }).catch((err)=>{
                        console.log(err);
                    });
                },
                //分页查询
                findPage() {
                    var param = {
                        currentPage: this.pagination.currentPage,
                        pageSize:this.pagination.pageSize,
                        queryString:this.pagination.queryString
                    };
                    axios.defaults.withCredentials = true;
                    axios.post(this.backendUrl+"/menu/findPage.do", param).then((res)=>{
                        console.log(res);
                        this.dataList = res.data.rows;
                        this.pagination.total = res.data.total;
                    }).catch((err)=>{
                        this.showMessage(err);
                    });
                },
                // 重置表单
                resetForm() {
                    if(this.$refs['dataAddForm'] != null) {
                        this.$refs['dataAddForm'].resetFields();
                    }
                    //清除表单的数据
                    this.formData =  {};
                    this.disabled =false;
                    this.disabled2 =false;
                },
                // 操作新建窗口
                /**
                 * 1. 窗口弹出
                 * 2. 重置表单
                 */
                handleCreate() {
                    this.dialogFormVisible = true;
                    this.resetForm();
                    this.findparentMenu();
                },
                findparentMenu(){
                    axios.defaults.withCredentials = true;
                    axios.get(this.backendUrl + "/menu/getAllOnemenu.do").then((res)=>{

                        console.log(res.data.data);
                        this.parentMenuList=res.data.data;
                        /*设置数据*/
                        this.$message({
                            type:res.data.flag ? "success" :"error",
                            message : res.data.message
                        });

                    }).catch((err)=>{
                        console.log(err);
                    });
                },
                // 编辑窗口
                /**
                 * 1. 弹出编辑窗口
                 * 2. 重置表单
                 * 3. 根据id查询检查项  ， 数据回显
                 */
                handleUpdate(row) {
                    // 1. 弹出编辑窗口
                    this.dialogFormVisible4Edit = true;
                    //2. 重置表单
                    this.resetForm();
                    // 3. 根据id查询检查项  ， 数据回显
                    axios.defaults.withCredentials = true;
                    axios.get(this.backendUrl + "/menu/findById.do?id="+row.id).then((res)=>{
                        //数据回显
                        this.formData = res.data.data;
                        this.findparentMenu();
                    }).catch((err)=>{
                        this.showMessage(err);
                    });
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;
                    this.findPage();
                },
                // 删除
                handleDelete(row) {
                    this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //点击的确定按钮
                        axios.get(this.backendUrl + "/menu/delById.do?id="+row.id).then((res)=>{
                            if(res.data.flag){
                                this.$message({
                                    type: 'success',
                                    message: res.data.message
                                });
                            }else{
                                this.$message({
                                    type: 'error',
                                    message: res.data.message
                                });
                            }
                            //重新查询
                            this.findPage();
                        }).catch((err)=>{
                            console.log(err);
                        });
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消删除'
                        });
                    });
                },
                showMessage(err){
                    if(err == 'Error: Request failed with status code 403'){
                        this.$message.error('权限不足，请联系管理员！！！');
                    }else{
                        this.$message.error('未知错误，请联系管理员！！！');
                    }
                }
            }
        })
    </script>
</html>